//
//  ViewController.m
//  TTMusic
//
//  Created by chenpengyu on 16/3/30.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "ViewController.h"
#import "TTMusicMusicGuanCollectionCell.h"
#import "TTMusicFindCollectionCell.h"
#import "TTMusicMyPageCollectionCell.h"
#import "MypageSearchViewController.h"
#import "YHYLoadSongViewController.h"
#import "CPYDiscoveryPageDataModel.h"
#import "YHYListViewController.h"
#import "YHYLocalViewController.h"
#import "YHYSincePlayViewController.h"
#import "YHYFavoriteViewController.h"
#import "YHY_MyPageMVViewController.h"
#import "ListenKnowMusicViewController.h"
#import "TTMusicRadioViewController.h"
#import "TTMusicRankListViewController.h"
#import "TTMusicSingerViewController.h"
#import "TTMusicSongListViewController.h"
#import "TuijianPushViewController.h"
#import "CPYFavouriteViewController.h"
#import "CPYMVViewController.h"
#import "CPYDiscoveryPageSongListModel.h"
#import "TTMusicSinglePlayerViewController.h"
#import "CPYMVPlayViewController.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, TTMusicMyPageCollectionCellDelegate, TTMusicFindCollectionCellDelegate, pushNextVCDelegate>


@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end



@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
//    创建上边的我的， 发现和推荐栏
    [self CreatHeaderView];
//    创建collection来承接3个视图
    [self CreatCollectionView];
//    左边导航栏点击事件，出现抽屉界面
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(AddButtonAction:)];
    [self creatDiscoveryPageModel];
//    我的页面跳转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jump:) name:@"jump" object:nil];
//    播放条发出的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(singleJump:) name:@"singleJump" object:nil];
//    最热MV 更多按钮点击事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jump) name:@"MVJump" object:nil];
    

}
#pragma mark - 跳转到播放VC
- (void)singleJump:(NSNotification *)notification {
    TTMusicSinglePlayerViewController *VC = [TTMusicSinglePlayerViewController sharePlayViewController];
    NSDictionary *dic = notification.object;
    CPYSONGSModel *songsModel = [dic objectForKey:@"CPYSONGSModel"];
    CPYDiscoveryPageSongListModel *songListModel = [dic objectForKey:@"CPYDiscoveryPageSongListModel"];
    VC.songsModel = songsModel;
    VC.songListModel = songListModel;
    VC.isRotation = [TTMusicAVPlayer shareTTMusicAVPlayer].isRoration;
    [VC.imageView sd_setImageWithURL:[NSURL URLWithString:songListModel.picUrl]];
    [self presentViewController:VC animated:YES completion:^{
    }];
    
}

#pragma mark - 我的页面的跳转
- (void)jump:(NSNotification *)notifition {
    
    NSNumber *num = [notifition object];
    if ([num  isEqual: @1]) {
        YHYLoadSongViewController *loadSongVC = [[YHYLoadSongViewController alloc] init];
        [self.navigationController pushViewController:loadSongVC animated:YES];
    }else if([num isEqual:@0]) {
        YHYLocalViewController *localVC = [[YHYLocalViewController alloc] init];
        [self.navigationController pushViewController:localVC animated:YES];
        
    }else if([num isEqual:@2]) {
        
        YHYSincePlayViewController *sinceVC = [[YHYSincePlayViewController alloc] init];
        [self.navigationController pushViewController:sinceVC animated:YES];
        
    }else if ([num isEqual:@3]) {
        
        YHYFavoriteViewController *favoriteVC = [[YHYFavoriteViewController alloc] init];
        [self.navigationController pushViewController:favoriteVC animated:YES];
        
    }else if ([num isEqual:@4]) {
        
        YHY_MyPageMVViewController *mvVC = [YHY_MyPageMVViewController shareMVPlayViewController];
        [self.navigationController pushViewController:mvVC animated:YES];
        
    }else {
        ListenKnowMusicViewController *listenVC = [[ListenKnowMusicViewController alloc] init];
        [self.navigationController pushViewController:listenVC animated:YES];
    }
    
}
#pragma mark - 标签栏左边点击事件
-(void)AddButtonAction:(UIBarButtonItem *)sender {
    
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (tempAppDelegate.LeftSlideVC.closed) {
        [tempAppDelegate.LeftSlideVC openLeftView];
    }
    else {
        
        [tempAppDelegate.LeftSlideVC closeLeftView];
    }
    
}

-(void)CreatHeaderView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    
    self.seg = [[UISegmentedControl alloc]initWithItems:@[@"我的",@"音乐馆", @"发现"]];
    self.navigationItem.titleView = self.seg;
    
    self.seg.frame = CGRectMake(0, 0, self.view.bounds.size.width * 0.5, 20);
    self.seg.selectedSegmentIndex = 1;
    
    self.seg.tintColor = [UIColor clearColor];
    
    NSDictionary *dicOne = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor lightGrayColor]};
    
    [self.seg setTitleTextAttributes:dicOne forState:UIControlStateNormal];
    NSDictionary *dicTwo = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor colorWithRed:0.91 green:1.0 blue:0.9668 alpha:1.0]};
    [self.seg setTitleTextAttributes:dicTwo forState:UIControlStateSelected];
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    
}

//用来关联点击seg来改变collection的cell视图
-(void)segAction:(UISegmentedControl *)sender {
    
    NSInteger index = _seg.selectedSegmentIndex;
    
    self.collectionView.contentOffset = CGPointMake(index * self.view.frame.size.width, 0);
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat x = scrollView.contentOffset.x / self.view.frame.size.width;
    self.seg.selectedSegmentIndex = x;
    
}

-(void)CreatCollectionView {
    
    self.navigationController.navigationBar.translucent = NO;
    
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc]init];
    flowLayOut.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
    flowLayOut.minimumLineSpacing = 0;
    flowLayOut.minimumInteritemSpacing = 0;
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayOut];
    self.collectionView.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
//    创建第一个我的界面cell
    [self.collectionView registerClass:[TTMusicMyPageCollectionCell class] forCellWithReuseIdentifier:@"TTMusicMyPageCollectionCellIdentifier"];
//    创建第二个音乐馆界面cell
    [self.collectionView registerClass:[TTMusicMusicGuanCollectionCell class] forCellWithReuseIdentifier:@"TTMusicMusicGuanCollectionCellIdentifier"];
//    创建发现界面的cell
    [self.collectionView registerClass:[TTMusicFindCollectionCell class] forCellWithReuseIdentifier:@"TTMusicFindCollectionCellIdentifier"];
    
    self.collectionView.contentOffset = CGPointMake(self.view.bounds.size.width, 0);
    
    [self.view addSubview:self.collectionView];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 0) {
        TTMusicMyPageCollectionCell *MyPageCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TTMusicMyPageCollectionCellIdentifier" forIndexPath:indexPath];
        MyPageCell.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
        MyPageCell.delegate = self;
        return MyPageCell;
        
    }if (indexPath.item == 1) {
        TTMusicMusicGuanCollectionCell *MusicGuanCell =[collectionView dequeueReusableCellWithReuseIdentifier:@"TTMusicMusicGuanCollectionCellIdentifier" forIndexPath:indexPath];
        MusicGuanCell.delegate = self;
        return MusicGuanCell;
    }

    TTMusicFindCollectionCell *FindCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TTMusicFindCollectionCellIdentifier" forIndexPath:indexPath];
    FindCell.delegate = self;
    FindCell.dataSource = _dataSource;
    return FindCell;
    
}
#pragma mark - 排行榜 电台 歌单 歌手列表跳转
- (void)jumpWithItem:(NSInteger)item withSection:(NSInteger)section {
    if (section == 1) {
        if (item == 0) {
            NSLog(@"%ld", item);
            TTMusicRankListViewController *rankListVC = [[TTMusicRankListViewController alloc] init];
            [self.navigationController pushViewController:rankListVC animated:YES];
        }else if (item == 1) {
            NSLog(@"%ld", item);
            TTMusicSongListViewController *songListVC = [[TTMusicSongListViewController alloc] init];
            songListVC.type = @"全部>";
            [self.navigationController pushViewController:songListVC animated:YES];
        }else if (item == 2) {
            NSLog(@"%ld", item);
            TTMusicRadioViewController *radioVC = [[TTMusicRadioViewController alloc] init];
            [self.navigationController pushViewController:radioVC animated:YES];
        }else if (item == 3) {
            NSLog(@"%ld", item);
            TTMusicSingerViewController *singerVC = [[TTMusicSingerViewController alloc] init];
            [self.navigationController pushViewController:singerVC animated:YES];
        }
    }
    
}
#pragma mark - 发现页面跳转
- (void)jumpWithItem:(NSInteger)item withSection:(NSInteger)section withSongId:(NSString *)Id {
    if (section == 3 || section == 4) {
        CPYFavouriteViewController *favouriteVC = [[CPYFavouriteViewController alloc]init];
        favouriteVC.songListId = Id;
        [self.navigationController pushViewController:favouriteVC animated:YES];
    }
    if (section == 6) {
        self.collectionView.contentOffset = CGPointMake(SysWidth, 0);
        self.seg.selectedSegmentIndex = 1;
    }
    if (section == 7) {
//        CPYMVViewController *MVVC = [[CPYMVViewController alloc] init];
//        [self.navigationController pushViewController:MVVC animated:YES];
    }
}

- (void)push {
    MypageSearchViewController *searchVC = [[MypageSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        
    }
}

- (void)creatDiscoveryPageModel {
    [TTMusicAFNetworkTool GETNetWithUrl:@"http://api.dongting.com/frontpage/frontpage" body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        for (NSDictionary *dic in result[@"data"]) {
            CPYDiscoveryPageDataModel *dataModel = [[CPYDiscoveryPageDataModel alloc] initWithDataSource:dic];
            [_dataSource addObject:dataModel];
        }
        [_collectionView reloadData];
    } failure:^(NSError *error) {
    }];
}
//推荐跳转到下一页的协议方法
-(void)pushNextVCWithDataSource:(NSMutableArray *)DataSource WithIndex:(NSInteger)index {
    
    TuijianPushViewController *tuijianPushVC = [[TuijianPushViewController alloc]init];
    
    tuijianPushVC.tuiJianFirstModel = DataSource[index];
    
    [self.navigationController pushViewController:tuijianPushVC animated:YES];
    
}
#pragma mark - 最热MV更多按钮跳转
- (void)jump {
    CPYMVViewController *MVVC = [[CPYMVViewController alloc] init];
    [self.navigationController pushViewController:MVVC animated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"bottomViewHidden" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
